package com.springcloud.client3tcc.web.service;


import com.codingapi.txlcn.tc.annotation.DTXPropagation;
import com.codingapi.txlcn.tc.annotation.TccTransaction;
import com.codingapi.txlcn.tracing.TracingContext;
import com.springcloud.client3tcc.web.dao.UserDao;
import com.springcloud.client3tcc.web.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author liq
 * @Title: UserService
 * @ProjectName lcn-demo
 * @Description: TODO
 * @date 2019-06-0409:21
 */
@Service
@Slf4j
public class UserService {
    @Autowired
    private UserDao userDao;


    @TccTransaction(propagation = DTXPropagation.SUPPORTS,cancelMethod = "cancelInsert",confirmMethod = "confirmInsert" ,executeClass = UserService.class)
    @Transactional
    public int insert(User user ){
        return userDao.insert(user.getName(),user.getUsername(),user.getPassword(),user.getGroupId());
    }

    public void confirmInsert(User user) {
        log.info("成功");
    }

    public void cancelInsert(User user) {
        log.error("失败,正在回退");
        userDao.delete(TracingContext.tracing().groupId());
        log.error("回退成功");
    }

}
